<template>
  <!-- 导入基本数据 -->
  <a-modal
    :visible="importExeclVisible"
    title="补贴面积数据待确认表"
    centered
    :closable="false"
    width="80%"
    dialogClass='member-execl-dialog'
    @cancel="importExeclVisible = false"
  >
    <ImportExeclPreview :execlData="execlData" ref="ImportExeclPreview" />
    <section slot="footer">
      <a-button
        @click="importExeclVisible = false"
      >取消</a-button>
      <a-button
        type="primary"
        :loading="importExeclConfirmLoading"
        @click="handlerConfirmImport"
      >{{ isPreview ? '待上传文件内容已确认' : '确认导入' }}</a-button>
    </section>
  </a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import { ImportExecl } from '@/utils/execl'
import { famerServices, IMember } from '@/common/services/famer'
import ImportExeclPreview from './GrainImportExeclPreview.vue'
import { message, Modal } from 'ant-design-vue'
import { BUSINESS_CODE } from '@/api/interceptors'
import MemberImportErrors from '../list/MemberImportErrors.vue'

@Component({
  components: {
    ImportExeclPreview,
    MemberImportErrors
  }
})
export default class MemberImportMixin extends Vue {
  @Prop({
    type: Boolean
  })
  public isPreview!: boolean
  public file!: File
  // 上传execl
  public importExeclVisible = false
  public importExeclConfirmLoading = false
  public execlData: any[] = []
  public handlerUploadExecl() {
    const _self = this
    ImportExecl({
      member_id: '农户编号',
      size1: '补贴面积',
      size2: '基础面积',
      size3: '扣除面积',
      size4: '增加面积',
    }, (data, map) => {
      // console.log('data', data)
      // const titles = Object.values(data[1])
      console.log(data)
      return data.slice(3).map((item: any) => {
        return {
          '农户编号': item['__EMPTY'],
          '补贴面积': item['__EMPTY_2'],
          '基础面积': item['__EMPTY_3'],
          '扣除面积': item['__EMPTY_4'],
          '增加面积': item['__EMPTY_5']
        }
      })

    }).then(_ => {
      this.importExeclVisible = true
      // @ts-ignore
      this.execlData = _.formatedData || [];
      // @ts-ignore
      this.file = _.file
    })
  }

  public async handlerConfirmImport() {
    // @ts-ignore
    const data = this.$refs.ImportExeclPreview.execlData
    if (!data.length) {
      message.error("请导入数据")
      return false
    }
    this.importExeclConfirmLoading = true

    // 预览模式下直接返回数据 + 关闭
    if (this.isPreview) {
      this.$emit('previewSuccess', {
        data,
        file: this.file
      })
      this.importExeclVisible = false
      return
    }
    
    try {
      // const step = 1000;
      // const count = Math.ceil(data.length / step);
      // for (let i = 0; i < count; i++) {
      //   const temp = data.slice(i * step, (i + 1) * step)
      //   await famerServices.importGrainExecl(temp)
      // }
      const _ = await famerServices.importGrainExecl({
        data,
        file_nam: this.file.name
      })
      const res = _.data
      if (res.code === BUSINESS_CODE.SUCCESS) {
        Modal.success({
          content: res.data || '数据导入成功',
          centered: true
        })
      } else {
        this.$dialog(MemberImportErrors, {
          messages: res.msg.map(str => ({ content: str }))
        }, {
          title: '异常补贴面积数据列表',
          width: '60%',
          footer: null
        })
        // message.error({
        //   content: res.msg
        // })
      }
    } finally {
      this.importExeclConfirmLoading = false
      this.importExeclVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.member-container {
  display: flex;
  .area-tree {
    width: 300px;
    flex: none;
    background-color: #fff;
    border-right: 1px solid #eee;
    margin-right: 10px;
  }
}
.house-holder /deep/ .ant-card-body {
  max-height: 600px;
  height: auto;
}
.member-list {
  display: flex;
  flex-direction: column;
}
</style>
